package org.dromara.house.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.house.domain.bo.ParkHousingBo;
import org.dromara.house.domain.bo.ParkHousingFileBo;
import org.dromara.house.domain.dto.DetailsDto;
import org.dromara.house.domain.dto.ParkHousingDto;
import org.dromara.house.domain.entity.ParkFile;
import org.dromara.house.domain.entity.ParkHousing;
import org.dromara.house.service.ParkHousingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 房源表 前端控制器
 * </p>
 *
 * @author LCH
 * @since 2025-08-26
 */
@RestController
@RequestMapping("/park/housing")
public class ParkHousingController extends BaseController {

    @Autowired
    private ParkHousingService service;

    /**
     * 获取房源表列表
     */
    @SaCheckPermission("park:housing:list")
    @GetMapping("/list")
    public TableDataInfo<ParkHousing> list(ParkHousingBo bo, PageQuery pageQuery) {
        return service.selectPageList(bo, pageQuery);
    }

    /**
     * 获取房源所有列表，不分页
     * 
     * @param bo
     * @return
     */
    @SaCheckPermission("park:housing:list")
    @GetMapping("/listAll")
    public List<ParkHousing> list(ParkHousingBo bo) {
        LambdaQueryWrapper<ParkHousing> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParkHousing::getDeleteStatus, 0);
        queryWrapper.eq(ObjectUtil.isNotEmpty(bo.getDistrictId()), ParkHousing::getDistrictId, bo.getDistrictId());
        queryWrapper.eq(ObjectUtil.isNotEmpty(bo.getHousingName()), ParkHousing::getHousingName, bo.getHousingName());
        queryWrapper.orderByDesc(ParkHousing::getCreateTime);
        return service.list(queryWrapper);
    }

    /**
     * id获取详细信息
     */
    // @SaCheckPermission("system:housing:query")
    @GetMapping(value = "/{id}")
    public R<ParkHousing> getInfo(@PathVariable Long id) {
        return R.ok(service.selectById(id));
    }

    /**
     * 获取对应楼层企业信息
     */
    // @SaCheckPermission("system:housing:query")
    @GetMapping(value = "/getDetails")
    public R<DetailsDto> getDetails(ParkHousingBo bo) {
        return R.ok(service.getDetails(bo));
    }

    /*
     * *
     * 新增房源表
     */
    @SaCheckPermission("system:housing:add")
    @Log(title = "房源表管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody ParkHousing parkHousing) {
        return toAjax(service.insertParkHousing(parkHousing));

    }

    /*
     * *
     * 修改房源表
     *
     */
    @SaCheckPermission("system:housing:edit")
    @Log(title = "房源表管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Void> edit(@Validated @RequestBody ParkHousing parkHousing) {
        return toAjax(service.updateParkHousing(parkHousing));
    }

    /**
     * 删除房源表
     **/
    @SaCheckPermission("system:housing:remove")
    @Log(title = "房源表管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 上传实景图
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public R<ParkFile> uploadImage(@RequestParam MultipartFile file) {
        return service.uploadImage(file);
    }

    /**
     * 保存房源对应的实景图
     *
     * @param bo
     * @return
     */
    @PostMapping("/saveImage")
    public R<Void> saveImage(@RequestBody ParkHousingFileBo bo) {
        return service.saveImage(bo);
    }

    /**
     * 删除实景图
     *
     * @param bo
     * @return
     */
    @PostMapping("/deleteImage")
    public R<Void> deleteImage(@RequestBody ParkHousingFileBo bo) {
        return service.deleteImage(bo);
    }

    /**
     * 获取实景图
     *
     * @param response
     */
    @PostMapping("/downloadImage/{fileId}")
    public void downloadImage(HttpServletResponse response, @PathVariable String fileId) {
        service.downloadImage(fileId, response);
    }

    @GetMapping("/downloadImage2/{fileId}")
    public void getDownloadImage(HttpServletResponse response, @PathVariable String fileId) {
        service.downloadImage(fileId, response);
    }
}
